import os, random, time
from msvcrt import getch
import menu, fov, levels
'''
FOV

    ###
  #######
 #########
 #########
###########
#####@#####
###########
 #########
 #########
  #######
    ###

#####
#####
##@##
#####
#####
'''
#KEYS
key_up=56
key_down=50
key_left=52
key_right=54

key_up_left=55
key_up_right=57
key_down_left=49
key_down_right=51

key_exit=27


char_char='@'
char=[1,1]
lvl=levels.level('lvl_test')
def show():
    index=[0,0]
    for i in lvl:
        index[1]=0
        for g in lvl[index[0]]:
            if [index[0],index[1]] in fov.fov(char):
                print g,
            else:
                print ' ',
            index[1]+=1
        index[0]+=1
        print ''

def place_char(char=char):
    lvl[char[0]][char[1]]=char_char

def clear(numlines=100):
    if os.name == "posix":
        os.system('clear')
    elif os.name in ("nt", "dos", "ce"):
        os.system('CLS')
    else:
        print '\n' * numlines

def tick():
    clear()
    show()

def right():
    if lvl[char[0]][char[1]+1]=='.':
        lvl[char[0]][char[1]+1]=char_char
        lvl[char[0]][char[1]]='.'
        char[1]+=1
    index=0
    
    tick()

def left():
    if lvl[char[0]][char[1]-1]=='.':
        lvl[char[0]][char[1]-1]=char_char
        lvl[char[0]][char[1]]='.'
        char[1]-=1
    tick()

def up():
    if lvl[char[0]-1][char[1]]=='.':
        lvl[char[0]-1][char[1]]=char_char
        lvl[char[0]][char[1]]='.'
        char[0]-=1
    tick()

def down():
    if lvl[char[0]+1][char[1]]=='.':
        lvl[char[0]+1][char[1]]=char_char
        lvl[char[0]][char[1]]='.'
        char[0]+=1
    tick()
    
def up_left():
    if lvl[char[0]-1][char[1]-1]=='.':
        lvl[char[0]-1][char[1]-1]=char_char
        lvl[char[0]][char[1]]='.'
        char[0]-=1
        char[1]-=1
    elif lvl[char[0]-1][char[1]]=='.':
        up()
    elif lvl[char[0]][char[1]-1]=='.':
        left()
    tick()
    
def up_right():
    if lvl[char[0]-1][char[1]+1]=='.':
        lvl[char[0]-1][char[1]+1]=char_char
        lvl[char[0]][char[1]]='.'
        char[1]+=1
        char[0]-=1
    elif lvl[char[0]-1][char[1]]=='.':
        up()
    elif lvl[char[0]][char[1]+1]=='.':
        right()
    tick()
    
def down_left():
    if lvl[char[0]+1][char[1]-1]=='.':
        lvl[char[0]+1][char[1]-1]=char_char
        lvl[char[0]][char[1]]='.'
        char[0]+=1
        char[1]-=1
    elif lvl[char[0]+1][char[1]]=='.':
        down()
    elif lvl[char[0]][char[1]-1]=='.':
        left()
    tick()
    
def down_right():
    if lvl[char[0]+1][char[1]+1]=='.':
        lvl[char[0]+1][char[1]+1]=char_char
        lvl[char[0]][char[1]]='.'
        char[0]+=1
    elif lvl[char[0]+1][char[1]]=='.':
        down()
    elif lvl[char[0]][char[1]+1]=='.':
        right()
    tick()
    
a=menu.menu()
b=a.__main__()
place_char()
show()
while True:
    time.sleep(0.1)
    z = getch()
    if ord(z) == key_left:
        left()
    elif ord(z) == key_right:
        right()
    elif ord(z) == key_up:
        up()
    elif ord(z) == key_down:
        down()
    elif ord(z) == key_exit:
        break
